package org.noka.nokatabmenu;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.noka.gson.Gson;
import org.nokatag.dbgrid.GridBoxItem;
import org.nokatag.system.BugInfoWrint;
import org.nokatag.tagjava.NokaTag;

public class NTab extends  NokaTag{
	private static final long serialVersionUID = -290294014590642413L;
	private String id = null;//div id
	private String optab = null;//初始化打开的菜单id
	private String width = "100%";//宽度
	private String height = "600px";//高度
	public String onselect = null;//在选中时调用的js方法
	
	private  JspWriter jspOut =null;
	private Set<TabItem> tabitems = null;
	private Set<GridBoxItem> GridIds = new LinkedHashSet<GridBoxItem>();//Grid
	
	public int doStartTag()throws JspException{
		try{
			jspOut= pageContext.getOut();
			tabitems = new LinkedHashSet<TabItem>();//ids
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			StringBuilder body = new StringBuilder();
			body.append(potjs(request));//js框架
			body.append(ntabJS(request));//select js
			body.append("<div class=\"noka_tab\" id=\""+id+"\" style=\"width:"+width+";height:"+height+"\">");
			//---------------选项卡标题-------------------------------------------------------------
			body.append("<div class=\"div_title_banner\">");
			body.append("<div id=\""+id+"_div_title\" class=\"div_title\">");
			body.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"  class=\"title_table\"><tr id=\""+id+"_tab_title_id_div\"></tr></table>");// id="'+self.id+'_tab_title_id_div"></div>';//标题
			body.append("</div>");
			body.append("</div>");
			body.append("<div  style=\"clear: both;\" class=\"line\"></div>");//分隔线
			body.append("<div id=\""+id+"_tab_body\" style=\"overflow:visible\">");//内容
			jspOut.write(body.toString());
		}catch(Exception se){
			 BugInfoWrint.Print(se);
		}
		return EVAL_BODY_INCLUDE;
	}
	public int doEndTag() throws JspException {
		try {
			StringBuilder body = new StringBuilder();
			body.append("</div></div>");
			body.append("<script type=\"text/javascript\">");
			//---------------显示树形菜单---------------------------
			body.append("Event.observe(document, 'dom:loaded', function() {");
			//--------------------------------------------------------------------
			body.append("var sfg"+id+" = {id:'"+id+"',");
			if(null!=optab){
				body.append("optab:'"+optab+"',");
			}
			if(!GridIds.isEmpty()){
				try{
					Gson gson = new Gson();
					body.append("gridids:"+gson.toJson(GridIds)+",");
				}catch(Exception se){}
			}
			if(null!=onselect){
				body.append("onselect:function(a){"+onselect+"(a);},");
			}
			Gson gson = new Gson();
			body.append("tabs:"+gson.toJson(tabitems));
			body.append("};");
			body.append("var nktab"+id+" = new nokatab(sfg"+id+");");
			body.append("nktab"+id+".show();");
			body.append("});");
			body.append("</script>");
			//---------------显示形菜单---------------------------
			pageContext.getOut().write(body.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		return EVAL_PAGE;
	}
	public void addGrid(GridBoxItem grid){
		GridIds.add(grid);
	}
	public void insertTab(TabItem tabitem){
		tabitems.add(tabitem);
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getOptab() {
		return optab;
	}
	public void setOptab(String optab) {
		this.optab = optab;
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getHeight() {
		return height;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	public String getOnselect() {
		return onselect;
	}
	public void setOnselect(String onselect) {
		this.onselect = onselect;
	}
	
	
	
}
